package com.javatest.chapter2;

import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author liuziyang
 * @create 2020-04-13 22:03
 */
public class AsyncThreadPoolExample {
    /**
     * 定义线程池
     */
    private final static int AVALIABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
    private final static ThreadPoolExecutor POOL_EXECUTOR = new ThreadPoolExecutor(AVALIABLE_PROCESSORS, AVALIABLE_PROCESSORS * 2, 1, TimeUnit.MINUTES, new LinkedBlockingDeque<>(5), new ThreadPoolExecutor.CallerRunsPolicy());


    public static String doSomethingA() {
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("--------doing something A----------");
        return "A task Done";
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.开启异步单元执行任务A
        Future<String> future = POOL_EXECUTOR.submit(() -> doSomethingA());
        //2.同步等待执行结果
        System.out.println(future.get());
    }
}
